﻿using AlibabaCloud.SDK.Alidns20150109.Models;
using Aurora.TaskScheduler.Extension;
using Gateway.IApplication.DescribeDomain;
using Gateway.Infrastructure.Provider.Operator;
using Gateway.IQuery;
using Gateway.UI.Api.TaskJobs;

namespace Gateway.UI.Api.Controllers;

/// <summary>
/// 阿里域名服务
/// </summary>
public class DescribeDomainController : BaseController
{
    private readonly IOperatorProvider _operatorProvider;
    private readonly TaskJobUtil _taskJobUtil;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="operatorProvider"></param>
    /// <param name="taskJobUtil"></param>       
    public DescribeDomainController(
        IOperatorProvider operatorProvider, TaskJobUtil taskJobUtil)
    {
        _operatorProvider = operatorProvider;
        _taskJobUtil = taskJobUtil;
    }

    /// <summary>
    /// 分页列表
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost, Route("list/page")]
    public PageResult<DescribeDomainsResponseBody.DescribeDomainsResponseBodyDomains.
        DescribeDomainsResponseBodyDomainsDomain> GetList(Core.Ali.DomainRequest request)
    {
        var data = Core.Ali.DescribeDomains.GetList(request);
        var total = data.Body.TotalCount;
        var rows = data.Body.Domains.Domain;
        var result =
            new PageResult<DescribeDomainsResponseBody.DescribeDomainsResponseBodyDomains.
                DescribeDomainsResponseBodyDomainsDomain>()
            {
                Rows = rows,
                Total = total.ToInt()
            };
        return result;
    }

    /// <summary>
    /// 列表
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost, Route("record/list/page")]
    public PageResult<DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecords.
        DescribeDomainRecordsResponseBodyDomainRecordsRecord> GetRecordList(Core.Ali.DomainRequest request)
    {
        var data = Core.Ali.DescribeDomains.GetRecordList(request);
        var total = data.Body.TotalCount;
        var rows = data.Body.DomainRecords.Record;
        var result =
            new PageResult<DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecords.
                DescribeDomainRecordsResponseBodyDomainRecordsRecord>()
            {
                Rows = rows,
                Total = total.ToInt()
            };
        return result;
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost, Route("save")]
    public Task<bool> Save(Core.Ali.DomainRequest request)
    {
        request.UserId = _operatorProvider.GetOperator().OperatorId;
        _taskJobUtil.InvokeLater(typeof(DescribeDomainSaveJob), request);
        return Task.FromResult(true);
    }
}